home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / ubppmpqs.zip / ppmp4a.asm < prev    next >
Assembly Source File  |  1993-04-16  |  19KB  |  1,317 lines

  1. ;PPMP4a.ASM 2.0
  2. ;    MACHINE LANGUAGE SUBROUTINES
  3. ;    FOR PPMPQS
  4. ;    1991/92 by YUJI KIDA
  5. ;
  6. .386P
  7.  
  8. code    segment    use16
  9.     assume    cs:code,ds:code
  10.  
  11.     INCLUDE    UBP.MAC
  12.     include    ppmp.h
  13.  
  14.  
  15. inactivesteplog    equ    9
  16.  
  17.  
  18. org    100h
  19.  
  20. start:
  21.  
  22. ;branch
  23.  
  24.     MOV_AX    AR0        ;ARRAY[0] is the command
  25.     MOV    BX,OFFSET CMD_TBL
  26.     SHL    AX,1
  27.     ADD    BX,AX
  28.     JMP    CS:[BX]
  29.  
  30. CMD_TBL:
  31.     dw    INITIALIZE,GETPRIME,STORE_DATA,SETACTIVE,COUNTCOLUMN
  32.     dw    DELETEROW,GAUSS1,GAUSS2,CUTEXCESSROW,MAKERFB,GETMEMORYSIZE
  33.  
  34.     even
  35. maxlen        dw    0
  36. P_OFF        dw    ?
  37. P_SEG        dw    ?
  38. primeadrnow    dd    ?
  39.  
  40. W_OFF        dw    ?
  41. W_SEG        dw    ?
  42.  
  43. Drv_OFF        dw    ?
  44. Drv_SEG        dw    ?
  45.  
  46. FBsize        dw    ?
  47. primes        dw    ?
  48. rowstotal    dw    ?
  49. rowsnow        dw    ?
  50. columnsweight0    dw    ?
  51. excessrows    dw    ?
  52.  
  53. heavycolweight    dw    ?
  54.  
  55. offnow        dw    ?
  56. primenow    dw    ?
  57.  
  58. count0        dw    ?
  59. count1        dw    ?
  60. count2        dw    ?
  61.  
  62. inactivenow    dw    ?
  63. inactivenext    dw    ?
  64. inactivecount    dw    ?
  65.  
  66. primehandle    dw    ?
  67. decomhandle    dw    ?
  68. histhandle    dw    ?
  69.  
  70. ptrbuffer2    dw    ?
  71. ptrbuffer3    dw    ?
  72. length1        dw    ?
  73. length2        dw    ?
  74. spmem        dw    ?
  75.  
  76. didsomesw    dw    0
  77.  
  78.     align    4
  79. pointernow    dd    ?
  80. datanow        dd    ?
  81. columncount    dd    ?
  82.  
  83. counterbase    dd    ?
  84. pointerbase    dd    ?
  85. database    dd    ?
  86. activeflgbase    dd    ?
  87.  
  88.  
  89. ;
  90. ; get memory size
  91. ;
  92. ;command#=10
  93.  
  94. getmemorysize:
  95.     mov    esi,pmodeworkadr-4
  96.     mov    eax,19510701h
  97.     mov    edx,00000400h        ;1Kbytes
  98.     mov    cx,31*1024        ;max 31Mbytes
  99. checkmemory100:
  100.     inc    eax
  101.     add    esi,edx
  102.     mov    fs:[esi],eax
  103.     cmp    eax,fs:[esi]
  104.     loope    checkmemory100
  105.  
  106.     mov    esi,pmodeworkadr-4    ;chack again for cache memory
  107.     mov    eax,19510701h
  108.     mov    edx,00000400h        ;1Kbytes
  109.     mov    cx,31*1024        ;max 31Mbytes
  110. checkmemory110:
  111.     inc    eax
  112.     add    esi,edx
  113.     cmp    eax,fs:[esi]
  114.     loope    checkmemory110
  115.  
  116.     mov    eax,esi
  117.     sub    eax,edx
  118.     sub    eax,pmodeworkadr-4
  119.     shr    eax,10            ;Kbytes
  120.     mov    bx,AR0
  121.     mov    cs:[bx],ax
  122.     retf
  123.     
  124.  
  125. ;
  126. ; make reduced FactorBase file
  127. ; and revived Row number file
  128. ;
  129. ;command#=9
  130.  
  131. RFBhandle    dw    ?
  132. RFBFILENAME    db    'PPMPRFB.PPM',0
  133. ROWShandle    dw    ?
  134. ROWSFILENAME    db    'PPMPROWS.PPM',0
  135.  
  136. makeRFB:
  137.     mov    ax,cs
  138.     mov    ds,ax
  139.     mov    es,ax
  140.  
  141.     mov    ah,3eh            ;close history
  142.     mov    bx,[histhandle]
  143.     or    bx,bx
  144.     jz    short makeRFB0
  145.     int    21h
  146. makeRFB0:
  147.     mov    dx,offset RFBFILENAME
  148.     mov    ah,3ch            ;create handle
  149.     mov    cx,0
  150.     int    21h
  151.     jc    diskerror
  152.     mov    [RFBhandle],ax
  153.  
  154.     xor    dx,dx            ;counter
  155.     mov    ebx,[activeflgbase]
  156.     mov    cx,[FBsize]
  157. makeRFB10:
  158.     push    ebx
  159.     push    cx
  160.  
  161.     mov    ax,0ffffh        ;non use mark
  162.     cmp    byte ptr fs:[ebx],1
  163.     jne    short makeRFB20
  164.     mov    ax,dx
  165.     inc    dx
  166. makeRFB20:
  167.     push    dx
  168.     mov    bx,offset BUFFER
  169.     mov    [bx],ax
  170.     mov    dx,bx
  171.     mov    cx,2
  172.     mov    bx,[RFBhandle]
  173.     mov    ah,40h            ;write
  174.     int    21h
  175.     pop    dx
  176.     pop    cx
  177.     pop    ebx
  178.     jc    diskerror    
  179.     inc    ebx
  180.     loop    makeRFB10
  181.  
  182.     cmp    dx,[inactivenow]
  183.     jne    makeRFBdataerror
  184.  
  185.     push    dx            ;/*
  186.  
  187.     mov    ah,3eh            ;close
  188.     mov    bx,[RFBhandle]
  189.     or    bx,bx
  190.     jz    short makeROWS
  191.     int    21h
  192.  
  193. makeROWS:
  194.     mov    dx,offset ROWSFILENAME
  195.     mov    ah,3ch            ;create handle
  196.     mov    cx,0
  197.     int    21h
  198.     jc    diskerror
  199.     mov    [ROWShandle],ax
  200.  
  201.     xor    dx,dx            ;counter
  202.     mov    ebx,[pointerbase]
  203.     mov    cx,[FBsize]
  204. makeROWS10:
  205.     test    dword ptr fs:[ebx],deletedmark4B
  206.     jnz    short makeROWS20
  207.  
  208.     push    bx
  209.     push    cx
  210.     push    dx
  211.  
  212.     mov    bx,offset BUFFER
  213.     mov    ax,[FBsize]
  214.     sub    ax,cx
  215.     mov    [bx],ax
  216.     mov    dx,bx
  217.     mov    cx,2
  218.     mov    bx,[ROWShandle]
  219.     mov    ah,40h            ;write
  220.     int    21h
  221.     jc    diskerror
  222.  
  223.     pop    dx
  224.     pop    cx
  225.     pop    bx
  226.  
  227.     inc    dx
  228.  
  229. makeROWS20:
  230.     add    ebx,4
  231.     loop    makeROWS10
  232.  
  233.     pop    ax            ;*/
  234.     cmp    ax,dx
  235.     jae    short makeROWS30
  236.     mov    ax,dx
  237. makeROWS30:
  238.     add    ax,matrixunitsize-1
  239.     and    ax,matrixunitmask
  240.     shr    ax,1            ;reduced matrix size / 2
  241.  
  242. makeRFBret:
  243.     mov    bx,AR0
  244.     mov    word ptr [bx],ax
  245.  
  246.     mov    ah,3eh            ;close
  247.     mov    bx,[RFBhandle]
  248.     or    bx,bx
  249.     jz    short makeRFBskip
  250.     int    21h
  251.  
  252. makeRFBskip:
  253.     mov    ax,ss
  254.     mov    ds,ax
  255.     mov    es,ax
  256.     retf
  257.  
  258. makeRFBdiskerror:
  259.     mov    ax,8001h
  260.     jmp    makeRFBret
  261.  
  262. makeRFBdataerror:
  263.     mov    ax,8002h        ;data error
  264.     jmp    makeRFBret
  265.  
  266. ;
  267. ; cut excess rows
  268. ;
  269. ;command#=8
  270.  
  271. CUTEXCESSROW:
  272.     mov    ax,cs
  273.     mov    ds,ax
  274.     mov    es,ax
  275.  
  276.     mov    [excessrows],0
  277.     mov    ax,[rowsnow]
  278.     mov    dx,[FBsize]
  279.     add    dx,[inactivenow]
  280.     sub    dx,[columnsweight0]
  281.     add    dx,8            ;for safety
  282.     sub    ax,dx
  283.     jbe    short excessout        ;no excess
  284.     mov    [excessrows],ax
  285.  
  286.     mov    cx,ax            ;clear buffer
  287.     shl    cx,1
  288.     mov    di,offset BUFFER
  289.     xor    ax,ax
  290.     rep    stosw
  291.     dec    ax
  292.     stosw                ;sentinel
  293.  
  294.     mov    cx,[rowstotal]
  295.     mov    ebx,[pointerbase]
  296.     xor    dx,dx            ;row#
  297. excess10:
  298.     push    cx
  299.     mov    esi,fs:[ebx]
  300.     test    esi,deletedmark4B
  301.     jnz    short excess20
  302.  
  303.     mov    ax,fs:[esi]        ;weight
  304.     mov    di,offset BUFFER
  305.     cmp    ax,[di]
  306.     jb    short excess20
  307.     call    storethisrownumber
  308. excess20:
  309.     add    ebx,4
  310.     inc    dx
  311.     pop    cx
  312.     loop    excess10
  313.  
  314.     ;now delete highest weight rows
  315.  
  316.     mov    si,offset BUFFER
  317.     mov    cx,[excessrows]
  318. excess50:
  319.     xor    eax,eax
  320.     add    si,2
  321.     lodsw
  322.     or    ax,ax
  323.     jz    short excess60
  324.     shl    eax,2
  325.     add    eax,[pointerbase]
  326.     or    dword ptr fs:[eax],deletedmark4B
  327.     dec    [rowsnow]
  328. excess60:
  329.     loop    excess50
  330.     
  331. excessout:
  332.     mov    ax,[excessrows]
  333.     mov    bx,AR0
  334.     mov    [bx],ax
  335.  
  336.     mov    ax,ss
  337.     mov    ds,ax
  338.     mov    es,ax
  339.     retf
  340.  
  341. storethisrownumber:
  342.     push    ebx
  343. storethisrow10:
  344.     mov    ebx,[di+4]
  345.     cmp    ax,bx
  346.     jb    short storethisrow50
  347.     mov    [di],ebx
  348.     add    di,4
  349.     jmp    storethisrow10
  350. storethisrow50:
  351.     mov    [di],ax            ;weight
  352.     mov    [di+2],dx        ;row#
  353.     pop    ebx
  354.     ret
  355.  
  356.  
  357. ;
  358. ; GAUSSian elimination using the rows of weight 2
  359. ;
  360. ;command#=7
  361.  
  362. GAUSS2:
  363.     mov    ax,cs
  364.     mov    ds,ax
  365.     mov    es,ax
  366.     mov    [spmem],sp
  367.     mov    [didsomesw],0
  368.  
  369.     mov    ebx,[pointerbase]
  370.     mov    cx,[rowstotal]
  371. gauss210:
  372.     push    cx
  373.     push    ebx
  374.     mov    esi,fs:[ebx]
  375.     test    esi,deletedmark4b
  376.     jnz    short gauss220
  377.  
  378.     mov    ax,fs:[esi]        ;weight
  379.     cmp    ax,2
  380.     jne    short gauss220
  381.  
  382.     or    dword ptr fs:[ebx],deletedmark4B
  383.     dec    [rowsnow]
  384.     call    setpivotnumber
  385.     call    gauss2sub
  386.     call    appendhistory
  387. gauss220:
  388.     pop    ebx
  389.     add    ebx,4
  390.     pop    cx
  391.     loop    gauss210
  392.  
  393.     mov    ax,[didsomesw]
  394.     mov    bx,AR0
  395.     mov    [bx],ax
  396.  
  397.     mov    ax,ss
  398.     mov    ds,ax
  399.     mov    es,ax
  400.     retf
  401.  
  402.  
  403. gauss2sub:
  404.     mov    dx,fs:[esi+2]        ;dx = pivot less value
  405.     mov    ax,fs:[esi+4]        ;ax = pivot larger value
  406.     
  407.     mov    ebx,[pointerbase]
  408.     mov    cx,[rowstotal]
  409. gauss2submainlp:
  410.     push    cx            ;*
  411.     mov    esi,fs:[ebx]
  412.     test    esi,deletedmark4b
  413.     jnz    short gauss2sub20
  414.  
  415.     mov    cx,fs:[esi]
  416.     jcxz    short gauss2sub20
  417. gauss2sub12:
  418.     add    esi,2
  419.     cmp    ax,fs:[esi]
  420.     jb    short gauss2sub20
  421.     je    short gauss2sub30
  422.     loop    gauss2sub12
  423.  
  424. gauss2sub20:
  425.     add    ebx,4
  426.     pop    cx            ;*
  427.     loop    gauss2submainlp
  428.     ret
  429.  
  430. gauss2sub30:
  431.     push    eax
  432.     call    replacethis
  433.     call    memooperation
  434.     pop    eax
  435.     jmp    gauss2sub20
  436.  
  437. replacethis:
  438.     mov    edi,fs:[ebx]        ;top adr
  439.     mov    ecx,esi
  440.     sub    ecx,edi
  441.     shr    cx,1
  442.     dec    cx            ;data# to be checked
  443.     jz    short replacethisret
  444. replacethislp:
  445.     mov    ax,fs:[esi-2]
  446.     cmp    ax,dx
  447.     jb    short replacethisret
  448.     je    short cutthis2
  449.     mov    fs:[esi],ax
  450.     sub    esi,2
  451.     loop    replacethislp
  452. replacethisret:
  453.     mov    fs:[esi],dx
  454. replacethisret2:
  455.     ret
  456.  
  457. cutthis2:
  458.     mov    ax,fs:[edi]        ;weight
  459.     mov    cx,ax
  460.     sub    ax,2
  461.     mov    fs:[edi],ax
  462.     jz    replacethisret2
  463.     mov    eax,esi
  464.     sub    eax,edi
  465.     shr    ax,1
  466.     sub    cx,ax    
  467.     jz    replacethisret2
  468. cutthis2lp:
  469.     add    esi,2
  470.     mov    ax,fs:[esi]
  471.     mov    fs:[esi-4],ax
  472.     loop    cutthis2lp
  473.     jmp    replacethisret2
  474.  
  475. ;
  476. ; GAUSSian elimination using the rows of weight 1
  477. ;
  478. ;command#=6
  479.  
  480. GAUSS1:
  481.     mov    ax,cs
  482.     mov    ds,ax
  483.     mov    es,ax
  484.     mov    [spmem],sp
  485.     mov    [didsomesw],0
  486.  
  487. gauss1mainlp:
  488.     mov    ebx,[pointerbase]
  489.     mov    cx,[rowstotal]
  490. gauss110:
  491.     push    cx
  492.     push    ebx
  493.     mov    esi,fs:[ebx]
  494.     test    esi,deletedmark4b
  495.     jnz    short gauss120
  496.  
  497.     mov    ax,fs:[esi]        ;weight
  498.     cmp    ax,1
  499.     jne    short gauss120
  500. gaussweight1:
  501.     or    dword ptr fs:[ebx],deletedmark4B
  502.     dec    [rowsnow]
  503.     call    setpivotnumber
  504.     call    gauss1sub
  505.     call    appendhistory
  506. gauss120:
  507.     pop    ebx
  508.     add    ebx,4
  509.     pop    cx
  510.     loop    gauss110
  511.     mov    ax,[didsomesw]
  512.     mov    bx,AR0
  513.     mov    [bx],ax
  514. gauss1out:
  515.     mov    ax,ss
  516.     mov    ds,ax
  517.     mov    es,ax
  518.     retf
  519.  
  520. appendhistory:
  521.     mov    dx,offset BUFFER
  522.     mov    cx,[ptrbuffer2]
  523.     sub    cx,dx
  524.     jbe    short historyret
  525.     mov    bx,[histhandle]
  526.     mov    ah,40h            ;write
  527.     int    21h
  528.     jc    gauss1diskerror
  529.     mov    [didsomesw],1
  530. historyret:
  531.     ret
  532.  
  533.     
  534. setpivotnumber:
  535.     mov    eax,ebx            ;pivot element number
  536.     sub    eax,[pointerbase]
  537.     shr    eax,2
  538.     mov    di,offset BUFFER0
  539.     stosw
  540.     mov    [ptrbuffer2],di
  541.     ret
  542.  
  543.  
  544. gauss1sub:
  545.     mov    ax,fs:[esi+2]        ;ax = pivot value
  546.     mov    ebx,[pointerbase]
  547.     mov    cx,[rowstotal]
  548. gauss1submainlp:
  549.     push    cx            ;*
  550.     mov    esi,fs:[ebx]
  551.     test    esi,dele